<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" style="padding-top: 30px;padding-left: 10px;">
      <el-form-item>
        <el-input v-model="input" placeholder="请输入查询科室" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      :span-method="objectSpanMethod"
      border
      :header-cell-style="headClass"
      style="width: 100%"
    >
      <el-table-column prop="name" label="床位" />
      <el-table-column prop="cost" label="费用(每日)" />
      <el-table-column prop="keshi" label="科室" />
      <el-table-column prop="state" label="状态" />
      <el-table-column prop="bingren" label="病人" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >分配</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="分配床位" :visible.sync="dialogFormVisibleUpdate">
      <el-form
        ref="guahao"
        size="mini"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" width="50px" prop="name">
          <el-input v-model="form.name" :disabled="true" autocomplete="off" />
        </el-form-item>
        <el-form-item label="费用(每日)" width="50px" prop="cost">
          <el-input v-model="form.cost" :disabled="true" autocomplete="off" />
        </el-form-item>
        <el-form-item label="科室" prop="keshi">
          <el-input v-model="form.keshi" :disabled="true" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" :disabled="true" autocomplete="off" />
        </el-form-item>
        <el-form-item label="病人" prop="bingren">
          <el-select v-model="form.bingren" :remote-method="userlist" placeholder="请选择">
            <el-option
              v-for="item in userForm"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="医生" prop="doctorName">
          <el-select v-model="form.doctorName" :remote-method="doctorlist" placeholder="请选择">
            <el-option
              v-for="item in doctorForm"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="缴费" prop="qian">
          <el-input v-model="form.qian" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleUpdate = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getList, upxiu, shan, brsuo, getbyks, yssuo, tianjia } from '../../../api/bedtable'
export default {
  data() {
    return {
      dialogFormVisibleUpdate: false,
      tableData: [],
      input: '',
      form: {
        id: '',
        name: '',
        cost: '',
        departmentid: '',
        keshi: '',
        state: '',
        patientid: '',
        bingren: '',
        doctorName: '',
        qian: ''
      },
      userForm: {},
      doctorForm: {}
    }
  },
  mounted() {
    this.Lodwn()
    this.userlist()
    this.doctorlist()
  },
  methods: {
    headClass() {
      return 'text-align: center;background-color: gainsboro;'
    },
    onSubmit() {
      if ((this.input === '' || this.input == null)) {
        this.Lodwn()
      } else {
        getbyks({
          keshi: this.input
        }).then((res) => {
          if (res.success) {
            this.tableData = res.result
          }
        })
      }
    },
    handleEdit(index, row) {
      this.dialogFormVisibleUpdate = true
      this.form = row
    },
    userlist() {
      brsuo().then((res) => {
        if (res.success) {
          this.userForm = res.result
        }
      })
    },
    doctorlist() {
      yssuo().then((res) => {
        if (res.success) {
          this.doctorForm = res.result
        }
      })
    },
    handleDelete(index, row) {
      this.$confirm('此操作将清除病人床位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        shan({ id: row.id }).then((res) => {
          if (res.success) {
            this.Lodwn()
            this.$message({
              message: res.message,
              type: 'success'
            })
          } else {
            this.$message.error(res.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消清除'
        })
      })
    },
    update() {
      this.dialogFormVisibleUpdate = false
      upxiu(
        {
          id: this.form.id,
          keshi: this.form.keshi,
          bingren: this.form.bingren
        }
      ).then((res) => {
        if (res.success) {
          this.Lodwn()
          console.log(this.form)
          this.$message({
            message: res.message,
            type: 'success'
          })
        } else {
          this.$message.error(res.message)
        }
      })
      tianjia(
        {
          patientId: this.form.patientid,
          departmentId: this.form.departmentid,
          qian: this.form.qian,
          bed: this.form.name,
          state: this.form.state,
          doctorName: this.form.doctorName

        }
      ).then((res) => {
        if (res.success) {
          this.Lodwn()
          console.log(this.form)
          this.$message({
            message: res.message,
            type: 'success'
          })
        } else {
          this.$message.error(res.message)
        }
      })
    },
    Lodwn() {
      getList().then((res) => {
        if (res.success) {
          this.tableData = res.result
        }
      })
    }
  }
}
</script>

<style>

</style>
